﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PlatForm_Login : System.Web.UI.Page
{
	/// <summary>
	/// 用户控制类
	/// </summary>
	private PX.User.UserComponent clsUserComp = new PX.User.UserComponent();

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);

		clsUserComp.page = Page;

		LBtnLogin.Click += new EventHandler(LBtnLogin_Click);
		LBtnRegister.Click += new EventHandler(LBtnRegister_Click);
		LBtnLogout.Click += new EventHandler(LBtnLogout_Click);
	}

	void LBtnLogout_Click(object sender, EventArgs e)
	{
		Session.Clear();

		ShowView();
	}

	void LBtnRegister_Click(object sender, EventArgs e)
	{
		Response.Redirect("Register.aspx");
	}

	/// <summary>
	/// 登录按钮
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnLogin_Click(object sender, EventArgs e)
	{
		PX.User.UserComponent clsUserComp = new PX.User.UserComponent();
		PX.User.User clsUser = new PX.User.User();
		PX.UserGroup.UserGroupComponent clsUserGroupComp = new PX.UserGroup.UserGroupComponent();

		if (TbUsername.Text == "")
		{
			Common.ShowMessageAlert.ShowMessage(Page, "用户名不能为空！");
		}
		else
		{

			if (TbPassword.Text == "")
			{
				Common.ShowMessageAlert.ShowMessage(Page, "密码不能为空！");
			}
			else
			{
				if (clsUserComp.CheckLogin(TbUsername.Text, TbPassword.Text, ref clsUser))
				{

					Session["LoginUserID"] = clsUser.ID.ToString();
					Session["LoginUserRight"] = clsUserGroupComp.GetOne(clsUser.UserGroupID).ColumnIDs;
					Session["LoginUserDepartmentID"] = clsUser.DepartmentID;

					ShowView();
				}
				else
				{
					Common.ShowMessageAlert.ShowMessage(Page, "用户名或密码错！");
				}
			}
			
		}

	}

	protected void Page_Load(object sender, EventArgs e)
	{

		Master.MasterLabTitle.Text = "用户中心";

		ShowView();
	}

	/// <summary>
	/// 根据用户是否登录设置视图
	/// </summary>
	private void ShowView()
	{
		int nLoginUserID = 0;		// 登录的用户ID
		PX.User.User clsUser = new PX.User.User();
		PX.Asker.Entity clsAsker = new PX.Asker.Entity();
		PX.Asker.Component clsAskerComp = new PX.Asker.Component();
		PX.Expert.Expert clsExpert = new PX.Expert.Expert();
		PX.Expert.ExpertComponent clsExpertComp = new PX.Expert.ExpertComponent();

		if (Session["LoginUserID"] == null)
		{
			// 没有登录
			MVLogin.SetActiveView(VLogin);
		}
		else
		{
			// 已经登录

			nLoginUserID = Convert.ToInt32(Session["LoginUserID"]);

			clsUser = clsUserComp.GetOne(nLoginUserID);
			clsAskerComp.page = Page;
			clsExpertComp.page = Page;
			clsAsker = clsAskerComp.Query(nLoginUserID);
			if (clsAsker != null)
			{
				if (clsAsker.IsAnoynmous)
				{
					MVLogin.SetActiveView(VLogin);
				}
				else
				{
					MVLogin.SetActiveView(VLoginUserInfo);
					LabHaveLoginTitle.Text = "欢迎" + clsUser.Name + "回来！";
					HyperLinkQuestion.Visible = true;
					HyperLinkAnswer.Visible = false;

				}
			}
			else
			{
				clsExpert = clsExpertComp.QueryByUserID(nLoginUserID);

				if (clsExpert != null)
				{
					MVLogin.SetActiveView(VLoginUserInfo);
					LabHaveLoginTitle.Text = "欢迎" + clsUser.Name + "回来！";
					HyperLinkQuestion.Visible = false;
					HyperLinkAnswer.Visible = true;
				}
				else
				{
					MVLogin.SetActiveView(VLoginUserInfo);
					LabHaveLoginTitle.Text = "欢迎" + clsUser.Name + "回来！";
					HyperLinkQuestion.Visible = false;
					HyperLinkAnswer.Visible = false;
				}
				
			}
			
		}
	}
}
